package test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lizhichuan
 * @date 2024/7/3 11:46
 */
public class TreeNode {
    private String id;
    private String name;
    private String type; // 阶段、校验分类、校验对象
    private String parentId;
    private boolean enabled;
    private String status; // 初始化、成功、失败
    private List<TreeNode> children;

    public TreeNode(String id, String name, String type, String parentId) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.parentId = parentId;
        this.enabled = false;
        this.status = "初始化";
        this.children = new ArrayList<>();
    }

    public TreeNode() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    public void addChild(TreeNode child) {
        this.children.add(child);
    }

    // 更新节点状态的逻辑
    public void updateStatusFromLogs(List<LogEntry> logs) {
        updateSelfStatus(logs);
        if ("校验分类".equals(this.type)) {
            updateCategoryStatus(logs);
        } else if ("阶段".equals(this.type)) {
            updatePhaseStatus(logs);
        }
        for (TreeNode child : children) {
            child.updateStatusFromLogs(logs);
        }
    }

    private void updateSelfStatus(List<LogEntry> logs) {
        // 假设LogEntry类有getId(), getType()和getStatus()方法
        for (LogEntry log : logs) {
            if (this.id.equals(log.getId()) && "使能".equals(log.getType())) {
                this.enabled = true;
            } else if (this.id.equals(log.getId()) && "状态".equals(log.getType())) {
                this.status = log.getStatus();
            }
        }
    }

    private void updateCategoryStatus(List<LogEntry> logs) {
        boolean hasEnabled = false;
        boolean allSuccess = true;
        boolean anyFailed = false;

        for (TreeNode child : children) {
            if (child.enabled) hasEnabled = true;
            if (!"成功".equals(child.status)) allSuccess = false;
            if ("失败".equals(child.status)) anyFailed = true;
        }

        if (hasEnabled) {
            this.enabled = true;
        }
        if (allSuccess) {
            this.status = "成功";
        } else if (anyFailed) {
            this.status = "失败";
        }
    }

    private void updatePhaseStatus(List<LogEntry> logs) {
        // 类似updateCategoryStatus逻辑，根据子节点的status更新自身status
    }
}

// 假设的日志条目类
class LogEntry {
    private String id;
    private String type; // "使能"或"状态"
    private String status; // "初始化"、"成功"或"失败"

    public LogEntry() {
    }

    public LogEntry(String id, String type, String status) {
        this.id = id;
        this.type = type;
        this.status = status;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}